﻿using System;
using System.Collections.Generic;
using System.Web.Services;
using BLL;
using BLL.RF.ATETest;
using BLL.RF.PowerTest;
using BLL.RF.ProductCategory;
using BLL.RF.Repair;
using Model.RF.ATETest;
using Model.RF.PowerTest;
using Model.RF.ProductCategory;
using Model.RF.Repair;

/// <summary>
///Power_TestAndUpload_ForGuangDongXinCe_Service 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。 
// [System.Web.Script.Services.ScriptService]
public class Power_TestAndUpload_ForGuangDongXinCe_Service : System.Web.Services.WebService
{
    private PowerTest powt;
    private tb_result_DFL tb;
    private ProductType p;
    private ProductType_Flow pf;
    private ProductType_ZTE pz;
    private ClientSoft cs;
    private DebugTest dt;
    private ProductType_MultiBand pm;
    private PowerTest_temp powt_temp;
    private PowerTestItem pti;
    private RepairLock rl;

    public Power_TestAndUpload_ForGuangDongXinCe_Service()
    {
        powt = new PowerTest();
        tb = new tb_result_DFL();
        p = new ProductType();
        pf = new ProductType_Flow();
        pz = new ProductType_ZTE();
        cs = new ClientSoft();
        dt = new DebugTest();
        pm = new ProductType_MultiBand();
        powt_temp = new PowerTest_temp();
        pti = new PowerTestItem();
        rl = new RepairLock();
        //如果使用设计的组件，请取消注释以下行 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string HelloWorld()
    {
        return "Hello World";
    }

    [WebMethod(Description = "判断广东信测开发的功率测试客户端软件的版本是否有效")]
    public bool IsValid_ClientSoft(string csVersion)
    {
        return !string.IsNullOrEmpty(cs.GetClientSoftbyNV("广东信测功率测试软件", csVersion).CSName) && cs.GetClientSoftbyNV("广东信测功率测试软件", csVersion).IsValid;
    }

    [WebMethod(Description = "通过SN，从调试记录数据中，获取SN对应的产品类型S代码")]
    public string ProductTypeID_GetbySN_FromDebugTest(string productSN)
    {
        productSN = CommHelper.GetProductSN(productSN);
        if (dt.DebugTest_SelectStepbySN(productSN, "初调").Count > 0)
        {
            return dt.DebugTest_SelectStepbySN(productSN, "初调")[0].ProductTypeID;
        }
        return null;
    }

    [WebMethod(Description = "通过S代码，获取S代码对应的产品类型名称")]
    public string ProductTypeName_GetbyProductTypeID(string productTypeID)
    {
        return p.LoadEntity(productTypeID).ProductTypeName;
    }

    [WebMethod(Description = "大功率数据录入时，判断之前工序是否有合格记录")]
    public bool ProductType_Flow_HasValidSevenStep_ForPowerTest(string productSN, string productTypeID)
    {
        bool b = pf.ProductType_Flow_HasValidSevenStep(productSN, productTypeID, "PowerStep");
        //b = b && RepairLock_IsLock(productSN);
        return b; 
    }

    [WebMethod(Description = "功率测试时，通过历史数据，判定当前测试的‘测试阶段’信息")]
    public string GetTestStep_bySN(string productSN)
    {
        productSN = CommHelper.GetProductSN(productSN);
        int count = powt.GetPowerTestsbySN(productSN).Count;
        switch (count)
        {
            case 0: return "一次";
            case 1: return "直接复测";
            case 2: return "吹气后复测1";
            default: return "返修后复测" + (count - 1);
        }
    }

    [WebMethod(Description = "判断产品类型S代码是否合法，在PowerData_Upload方法之前调用")]
    public bool IsValidProductTypeID(string productTypeID)
    {
        return !string.IsNullOrEmpty(p.LoadEntity(productTypeID).ProductTypeID);
    }

    [WebMethod(Description = "上传摩比格式大功率测试数据")]
    public bool PowerData_Upload(PowerTestInfo pi, List<PowerTestItemInfo> list)
    {
        return powt.PowerTestAndItem_Insert(pi, (IList<PowerTestItemInfo>)list);
    }

    [WebMethod(Description = "是否需要上传中兴格式数据的产品类型，在ZTE_PowerTestData_Upload方法之前调用")]
    public bool Isvalid_ProductType_ZTE(string productTypeID)
    {
        return !string.IsNullOrEmpty(pz.LoadEntity(productTypeID).ProductTypeID);
    }

    [WebMethod(Description = "上传中兴格式的功率测试数据")]
    public bool ZTE_PowerTestData_Upload(tb_result_DFL_Info ti, string productTypeID, List<tb_Param_DFL_Info> items)
    {
        ti.IsPIM = false;
        return tb.tb_ATETestAndItem_Insert(ti, productTypeID, items);
    }


    #region 20171113日，增加多频段测试数据合并相关接口

    [WebMethod(Description = "根据产品类型S代码获取需测试的频段信息，返回为空或Count为1时，无需合并")]
    public List<ProductType_MultiBandInfo> GetMultiBandByProductTypeID(string productTypeID)
    {
        List<ProductType_MultiBandInfo> list = (List<ProductType_MultiBandInfo>)pm.ProductType_MultiBand_GetbyPID(productTypeID);
        if (list.Count == 0)
        {
            return null;
        }
        else
        {
            for (int i = list.Count - 1; i >= 0; i--)
            {
                ProductType_MultiBandInfo item = list[i];
                if (item.StepItem != "大功率测试")
                    list.Remove(item);
            }
            return list;
        }
    }

    [WebMethod(Description = "根据产品序列号，获取临时数据库中，对应不同频段最新的测试记录")]
    public List<PowerTest_tempInfo> GetLatestTestbyProductSN(string productSN)
    {
        productSN = CommHelper.GetProductSN(productSN);
        return new List<PowerTest_tempInfo>(powt_temp.PowerTest_temp_GetLatestbyProductSN(productSN));
    }

    [WebMethod(Description = "多频段产品的测试数据上传至临时数据库，操作成功返回true，否则返回false；tag参数标识是否屏蔽该产品所有历史数据")]
    public bool UploadData_TempDataBase(PowerTest_tempInfo ti, List<PowerTestItemInfo> list, bool tag)
    {
        ti.ProductSN = CommHelper.GetProductSN(ti.ProductSN);
        return tag ? powt_temp.PowerTest_tempAndItem_Insert(ti, (IList<PowerTestItemInfo>)list) && powt_temp.PowerTest_temp_DisableRecordsbySN(ti.ProductSN) : powt_temp.PowerTest_tempAndItem_Insert(ti, (IList<PowerTestItemInfo>)list);
    }

    [WebMethod(Description = "根据测试记录，获取相应测试详细数据，用于获取多份数据后合并")]
    public List<PowerTestItemInfo> GetTestItembyTestInfo(PowerTest_tempInfo ti)
    {
        return new List<PowerTestItemInfo>(pti.GetPowerTestItem_tempsbyID(ti.ID));
    }
    #endregion

    

    #region 20180125日，增加自动返修相关

    [WebMethod(Description = "测试不合格时，自动生成返修锁定，以限制重新走当前工序")]
    public bool RepairLock_AutoBuild(string productSN)
    {
        productSN = CommHelper.GetProductSN(productSN);
        RepairLockInfo rli = new RepairLockInfo() { ID = 0, ProductSN = productSN, RecordTime = DateTime.Now, StepItem = "PowerStep", IsValid = true };
        return rl.RepairLock_AutoBuild(rli);
    }

    [WebMethod(Description = "测试前，判断序列号在功率测试工序是否被锁定")]
    public bool RepairLock_IsLock(string productSN)
    {
        productSN = CommHelper.GetProductSN(productSN);
        return rl.RepairLock_ProductSNIsLock(productSN, "PowerStep");
    }
    #endregion
}

